
#include "MainDialog.h"

//////////////////////////////////////////////////////////////////////////

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//////////////////////////////////////////////////////////////////////////

CMainDialog::CMainDialog(CWnd *pParent)
	:	CDialog(CMainDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMainDialog)
	//}}AFX_DATA_INIT

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMainDialog::DoDataExchange(CDataExchange *pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMainDialog)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMainDialog, CDialog)
	//{{AFX_MSG_MAP(CMainDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

BOOL CMainDialog::OnInitDialog()
{
	CDialog::OnInitDialog();

	//////////////////////////////////////////////////////////////////////////

	SetIcon(m_hIcon, TRUE);
	SetIcon(m_hIcon, FALSE);

	//////////////////////////////////////////////////////////////////////////
	
	return TRUE;
}

void CMainDialog::OnPaint() 
{
	if (IsIconic()) {
		CPaintDC dc(this);

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);

		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		dc.DrawIcon(x, y, m_hIcon);
	}
	else {
		CDialog::OnPaint();

		CClientDC dc(this);

		Test(dc.GetSafeHdc());
	}
}

HCURSOR CMainDialog::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//////////////////////////////////////////////////////////////////////////

template <class FunDrawPoint>
void MatchDotMatrix(char *pTxt, long nTxtWidth, long nTxtHeight, FunDrawPoint funCallBack, unsigned long lUserData)
{
	char cFlag = 0;
	size_t nLineBytes = ((nTxtWidth >> 3) + ((nTxtWidth % 0x08) != 0));

	if (pTxt == NULL || funCallBack == NULL || lUserData == 0) {
		return;
	}

	for (long i = 0; i < nTxtHeight; ++i) {
		for (long j = 0; j < nLineBytes; ++j) {
			cFlag = pTxt[i * nLineBytes + j];
			if (cFlag == 0) {
				continue;
			}

			for (long k = 0; k < 8; ++k) {
				if (cFlag & 0x01) {
					funCallBack(j * 8 + (7 - k), i, lUserData);
				}

				cFlag >>= 1;

				if (cFlag == 0) {
					break;
				}
			}
		}
	}
}

struct Param
{
	HDC hDC;
	long x;
	long y;
};

void _DrawPoint(long x, long y, unsigned long lUserData)
{
	Param *pParam = (Param *) lUserData;
	SetPixel(pParam->hDC, pParam->x + x, pParam->y + y, RGB(0, 0, 0));
}

extern unsigned char szASC12[];
extern unsigned char szASC16[];
extern unsigned char szASC24[];
extern unsigned char szASC32[];
extern unsigned char szHZK12[];
extern unsigned char szHZK16[];
extern unsigned char szHZK24[];
extern unsigned char szHZK32[];

void DrawHZK(Param &param, long lWidth, long lHeight, unsigned char *pszZK)
{
	long nOneLineSize = lWidth / 8 + ((lWidth % 8) != 0);
	USHORT nBase = 0x0582;
	USHORT nCount= 0x0010;
	for (USHORT i=nBase; i<nBase+nCount; ++i) {
		if (param.x > 860) {
			param.x = 0;
			param.y += lHeight;
		}

		MatchDotMatrix((char *) (&(pszZK[i*(nOneLineSize*lHeight)])), lWidth, lHeight, _DrawPoint, (ULONG) &param);
		param.x += lWidth;
	}
	param.x = 0;
	param.y += lHeight;
}

void DrawASC(Param &param, long lWidth, long lHeight, unsigned char *pszZK)
{
	long nOneLineSize = lWidth / 8 + ((lWidth % 8) != 0);
	USHORT nBase = 0x0;
	USHORT nCount= 255;
	for (USHORT i=nBase; i<nBase+nCount; ++i) {
		if (param.x > 860) {
			param.x = 0;
			param.y += lHeight;
		}

		MatchDotMatrix((char *) (&(pszZK[i*(nOneLineSize*lHeight)])), lWidth, lHeight, _DrawPoint, (ULONG) &param);
		param.x += lWidth;
	}
	param.x = 0;
	param.y += lHeight;
}

void CMainDialog::Test(HDC hDC)
{
	Param param;
	param.hDC = hDC;
	param.x = 0;
	param.y = 0;

	DrawHZK(param, 12, 12, szHZK12);
	param.y += 4;
	DrawHZK(param, 16, 16, szHZK16);
	param.y += 4;
	DrawHZK(param, 24, 24, szHZK24);
	param.y += 4;
	DrawHZK(param, 32, 32, szHZK32);
	param.y += 4;

	DrawASC(param, 6, 12, szASC12);
	param.y += 8;
	DrawASC(param, 8, 16, szASC16);
	param.y += 8;
	DrawASC(param, 12, 24, szASC24);
	param.y += 8;
	DrawASC(param, 16, 32, szASC32);
	param.y += 8;
}
